﻿Imports BUS
Imports DAO
Imports DTO
Imports DevComponents.DotNetBar

Public Class frmPhieuNhapSach
    Public Shared MaSach As String

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Public Sub HienThiDanhSach()
        dgvChiTietDanhSachPhieuNhap.DataSource = PhieuNhapDAO.SelectPhieuNhapChiTietAll()
        dgvDanhSachPhieuNhap.DataSource = PhieuNhapDAO.SelectPhieuNhapAll()
    End Sub

    Public Sub HienThiMaSach()
        txtChiTietMaSach.Text = MaSach
    End Sub

    Private Sub frmPhieuNhapSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(435, 729)
        'ColCmbSach.DataSource = SachDAO.SelectSachAll()
        'ColCmbSach.ValueMember = "MaSach"
        'ColCmbSach.DisplayMember = "TenSach"
        'ColCmbTacGia.DataSource = SachDAO.SelectSachAll()
        'ColCmbTacGia.ValueMember = "MaSach"
        'ColCmbTacGia.DisplayMember = "TacGia"
        HienThiDanhSach()
        dtNgayNhap.Value = DateTime.Now

    End Sub

    Private Sub dgvChiTietDanhSachPhieuNhap_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvChiTietDanhSachPhieuNhap.SelectionChanged
        If dgvChiTietDanhSachPhieuNhap.SelectedRows.Count > 0 Then
            txtChiTietMaPhieuNhap.Text = dgvChiTietDanhSachPhieuNhap.SelectedRows(0).Cells(0).Value.ToString()
            txtChiTietMaSach.Text = dgvChiTietDanhSachPhieuNhap.SelectedRows(0).Cells(1).Value.ToString()
            txtSoLuong.Text = dgvChiTietDanhSachPhieuNhap.SelectedRows(0).Cells(2).Value.ToString()
        End If
    End Sub


    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim dem As Integer = dgvDanhSachPhieuNhap.Rows.Count
        Me.Size = New Size(877, 729)
        Dim p As New PhieuNhapDTO()
        p.NgayNhap = dtNgayNhap.Value

        If PhieuNhapBUS.themphieunhap(p) = False Then
            MessageBoxEx.Show("Thêm phiếu nhập thất bại")

        End If
        HienThiDanhSach()
        txtMaPhieuNhap.Text = dgvDanhSachPhieuNhap.Rows(dem).Cells(0).Value.ToString()
    End Sub

    Private Sub dgvDanhSachPhieuNhap_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachPhieuNhap.SelectionChanged
        If dgvDanhSachPhieuNhap.SelectedRows.Count > 0 Then
            txtChiTietMaPhieuNhap.Text = dgvDanhSachPhieuNhap.SelectedRows(0).Cells(0).Value.ToString()
            txtMaPhieuNhap.Text = dgvDanhSachPhieuNhap.SelectedRows(0).Cells(0).Value.ToString()
        End If
    End Sub

    Private Sub btnChiTietThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChiTietThem.Click
        Dim dt As DataTable = ThamSoDAO.GetThamSoAll()
        Dim nhapmin As Integer = Integer.Parse(dt.Rows(0).ItemArray(1).ToString())
        Dim luongtonmin As Integer = Integer.Parse(dt.Rows(0).ItemArray(2).ToString())

        Dim p As New PhieuNhapDTO()
        Try

            If txtChiTietMaPhieuNhap.Text <> "" Then
                p.MaPhieuNhap = Integer.Parse(txtChiTietMaPhieuNhap.Text)

            Else
                MessageBoxEx.Show("Mã phiếu nhập không để trống")
                Return
            End If
            If txtChiTietMaSach.Text <> "" Then

                p.MaSach = Integer.Parse(txtChiTietMaSach.Text)
            Else
                MessageBoxEx.Show("Mã sách không được để trống")
                Return
            End If
        Catch
            MessageBoxEx.Show("Mã là số nguyên")
            Return
        End Try
        Try
            If Integer.Parse(txtSoLuong.Text) < nhapmin Then
                MessageBoxEx.Show("Số lượng nhập phải lớn hơn số lượng qui định")
                Return
            Else
                p.SoLuong = Integer.Parse(txtSoLuong.Text)
            End If
        Catch e1 As FormatException
            MessageBoxEx.Show("Số lượng nhập vào là kiểu số")
            Return
        End Try
        Dim dt2 As DataTable = ThamSoDAO.SlectSoLuongTon(p.MaSach)
        Dim luongton As Integer = Integer.Parse(dt2.Rows(0).ItemArray(3).ToString())
        Dim soluongtonnew As Integer = luongton + Integer.Parse(txtSoLuong.Text)
        If luongton < luongtonmin Then
            If PhieuNhapBUS.themchitietphieunhap(p) = False Then
                MessageBoxEx.Show("Sách này dã có trong phiếu nhập!")
            Else

                Dim s As New SachDTO()
                s.MaSach = Integer.Parse(txtChiTietMaSach.Text)
                s.SoLuongTon = soluongtonnew
                SachDAO.UpdateSoLuongTon(s)
                HienThiDanhSach()
                Dim bc As New BaoCaoTonDTO()
                bc.MaSach = s.MaSach
                bc.NgayPhatSinh = dtNgayNhap.Value
                bc.PhatSinh = "Lập phiếu nhập sách"
                bc.TonDau = luongton
                bc.TonCuoi = soluongtonnew
                BaoCaoTonDAO.Insert(bc)
                MessageBoxEx.Show("Nhập thành công")
            End If
        Else
            MessageBoxEx.Show("Chỉ nhập các đầu sách có số lượng tồn nhỏ hơn qui định")
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        txtChiTietMaPhieuNhap.Clear()
        txtChiTietMaSach.Clear()
        txtSoLuong.Clear()
    End Sub

    Private Sub btnThemChiTiet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemChiTiet.Click
        Me.Size = New Size(877, 729)
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Size = New Size(435, 729)
    End Sub

    Private Sub btnTimMaSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimMaSach.Click
        Dim f As New frmDanhSachSach()
        f.ShowDialog()
        HienThiMaSach()
    End Sub
End Class